import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_app/BaseMaterialWidget.dart';

class CanvasDemo extends BaseMaterialWidget {
  @override
  Widget buildContent(BuildContext context) {
    return ListView(
      children: <Widget>[_drawLine(), _drawCircle()],
    );
  }

  Widget _drawLine() {
    return SizedBox(
      width: 500,
      height: 150,
      child: CustomPaint(
        painter: LinePainter(),
      ),
    );
  }

  Widget _drawCircle() {
    return SizedBox(
      width: 500,
      height: 700,
      child: CustomPaint(
        painter: CirclePainter(),
      ),
    );
  }

  @override
  String getTitle() {
    return "Canvas和Paint使用";
  }
}

class LinePainter extends CustomPainter {
  //定义画笔
  Paint _paint = Paint()
    ..strokeCap = StrokeCap.butt
    ..color = Colors.green
    ..strokeWidth = 3;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawLine(Offset(20, 20), Offset(300, 20), _paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

class CirclePainter extends CustomPainter {
  Paint _paint = Paint()
    ..strokeCap = StrokeCap.round
    ..color = Colors.blue
    ..style = PaintingStyle.fill
    ..strokeWidth = 3;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawCircle(Offset(150, 0), 100, _paint);

    //绘制点
    _paint.color = Colors.white;
    canvas.drawPoints(
        PointMode.points,
        [
          Offset(150, 0),
          Offset(100, 0),
          Offset(150, -50),
          Offset(150, 50),
          Offset(200, 0)
        ],
        _paint);

    canvas.drawLine(Offset(100, 0), Offset(200, 0), _paint);
    canvas.drawLine(Offset(150, -50), Offset(150, 50), _paint);

    //绘制椭圆
    _paint.color = Colors.green;
    _paint.style = PaintingStyle.stroke;
    canvas.drawOval(Rect.fromPoints(Offset(80, 100), Offset(300, 200)), _paint);

    //绘制圆角矩形
    canvas.drawRRect(
        RRect.fromLTRBR(80, 100, 280, 300, Radius.circular(16)), _paint);

    //绘制圆弧
    canvas.drawArc(Rect.fromCircle(center: Offset(150, 310), radius: 100), 0,
        pi / 2, true, _paint);

    //绘制路径
    Path _path = Path()
      ..moveTo(140, 310)
      ..lineTo(40, 310)
      ..moveTo(140, 310)
      ..lineTo(140, 410)
      ..arcTo(Rect.fromCircle(center: Offset(140, 310), radius: 100), pi / 2,
          pi / 2, false);
    canvas.drawPath(_path, _paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}
